<?php
namespace App\Http\Controllers\User;

use App\Libs\uploadImg;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Session\Store;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\DB;
use App\Models\LineModel;
use App\Models\AddressModel;
use App\Models\IndexHtmlModel;
use App\Models\EncyclopediasModel;
use App\Models\ProblemModel;
use App\Models\HotPhoneModel;
use App\Models\AboutUsModel;

class IndexController extends Controller{

    public function __construct()
    {
        $this->middleware('user_login');
    }

    //获取路线类型
    public function line(LineModel $lineModel){

        $data = $lineModel->get();
        return returnMsg(0,'获取成功',$data);

    }

    //获取国家地区
    public function address(Request $request,AddressModel $addressModel){

        $query = $addressModel
            ->select([
                'id',
                'address',
                'parent_id',
            ]);

        if(!empty($request->input('parent_id'))){
            $query->where('parent_id',$request->input('parent_id'));
        }else{
            $query->where('parent_id',0);
        }

        $data = $query->get();
        return returnMsg(0,'获取成功',$data);
    }

    /**
     *上传图片
     * @param $file
     * @return int|string
     */
    public function upload_img(Request $request)
    {

        $tmp = $request->file('file');
        $url = upload_img($tmp);
        return $data=[
            'http_url'=>getDomain().'/upload/'.$url,
            'url'=>'/upload/'.$url
        ];

    }

    //获取首页设置内容
    public function index_set(IndexHtmlModel $htmlModel){

        $data = $htmlModel->get();
        return returnMsg(0,'获取成功',$data);

    }

    //获取百科列表
    public function get_encyclopedias(EncyclopediasModel $encyclopediasModel){

       $data =  $encyclopediasModel->get();

        return returnMsg(0,'获取成功',$data);

    }

    //百科详情
    public function encyclopedias_info(Request $request,EncyclopediasModel $encyclopediasModel){

        $data =  $encyclopediasModel->where('id',$request->input('encyclopedias_id'))->first();

        return returnMsg(0,'获取成功',$data);

    }

    //常见问题
    public function get_problem(Request $request,ProblemModel $problemModel){

        $query =  $problemModel->select(['*']);

        if(!empty($request->input('keyword'))){
            $query->where('problem','like','%'.$request->input('keyword').'%');
        }

        $data = $query->get();

        return returnMsg(0,'获取成功',$data);

    }

    //百科详情
    public function problem_info(Request $request,ProblemModel $problemModel){

        $data =  $problemModel->where('id',$request->input('problem_id'))->first();

        return returnMsg(0,'获取成功',$data);

    }

    //获取热线
    public function hot_phone(HotPhoneModel $hotPhoneModel){

        $data = $hotPhoneModel->first();

        return returnMsg(0,'获取成功',$data);

    }

    //关于我们
    public function about_us(AboutUsModel $aboutUsModel){

        $data = $aboutUsModel->first();

        return returnMsg(0,'获取成功',$data);

    }

}